<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\ding;

use app\common\model\ding\SeatImages as SeatImagesModel;
use think\facade\Db;

/**
 * 商品评价模型
 * Class Article
 * @package app\api\model
 */
class SeatImages extends SeatImagesModel {
	/**
	 * 隐藏字段
	 * @var array
	 */
	protected $hidden = [
		'is_delete',
		'store_id',
		'create_time',
		'update_time',
	];

	public static function increased(int $seat_id, array $contentList) {

	
			foreach ($contentList as $field) {
				$data['file_id'] = $field;
				$data['seat_id'] = $seat_id;
				$data['create_time'] = time();
				$data['store_id'] = self::$storeId;
				
				$insertData[]=$data;
			}
			return (new static )->addAll($insertData);
			
	
	}

	/**
	 * 获取列表--转台
	 * @param int $categoryId
	 * @param int $limit
	 * @return \think\Paginator
	 * @throws \think\db\exception\DbException
	 */
	public function getListChange(int $shopId = 0, $params) {

		// 检索查询条件
		$filter = [];
		// 获取列表数据
		$lists = $this->with(['seat'])

			->where('shop_id', '=', $shopId)
			->where('is_delete', '=', 0)
			->order(['create_time' => 'desc'])
			->select()->toArray();

		foreach ($lists as $key => $list) {
			$seats = $list['seat'];
			foreach ($seats as $k => $value) {
				$order = Db::name('ding_order_seat')->alias('o')

					->join('ding_order order', 'order.id=o.order_id')
					->where('o.seat_id', $value['seat_id'])
					->where('o.status', 'in', [10])
					->where('o.date', $params['date'])
					->where('o.type', $params['type'])
					->where('o.is_delete', 0)
					->field('o.*')
					->find();

				$is_can = 1; //修改
				if ($order && $order['order_id'] != $params['orderId']) {
					$is_can = 2; //调换

				}
				if ($order && $order['order_id'] == $params['orderId']) {
					$is_can = 0; //不可修改
				}
				if (strtotime($params['date']) < strtotime(Date('Y-m-d'))) {
					$is_can = 0;
				}

				$seats[$k]['is_can'] = $is_can;
			}
			$lists[$key]['seat'] = $seats;
		}
		return $lists;
	}

	/**
	 * 获取列表--安排落座
	 * @param int $categoryId
	 * @param int $limit
	 * @return \think\Paginator
	 * @throws \think\db\exception\DbException
	 */
	public function getListYuyue(int $shopId = 0, $params) {
		if ($params['date'] == '') {
			$params['date'] = date('Y-m-d');
		}
		// 检索查询条件
		$filter = [];
		// 获取列表数据
		$lists = $this->with(['seat'])

			->where('shop_id', '=', $shopId)
			->where('is_delete', '=', 0)
			->order(['create_time' => 'desc'])
			->select()->toArray();

		foreach ($lists as $key => $list) {
			$seats = $list['seat'];
			$newArr = [];
			foreach ($seats as $k => $value) {
				$order = Db::name('ding_order_seat')->alias('o')
					->join('store_shop_clerk u', 'u.clerk_id=o.clerk_id')
					->join('ding_letter l', 'l.type_id=o.letter_type')
					->where('o.seat_id', $value['seat_id'])
					->where('o.status', 'in', [10, 20])
					->where('o.date', $params['date'])
					->where('o.type', $params['type'])
					->where('o.is_delete', 0)
					->field('o.*,u.real_name as clerk_name,l.name as letter_type')
					->find();

				$seats[$k]['status'] = $order ? $order['status'] : 0;
				$is_past = 0;
				if ($seats[$k]['status'] == 10 || $seats[$k]['status'] == 20) {

				} else {
					$newArr[] = $value;
				}

			}
			$lists[$key]['seat'] = $newArr;
		}
		return $lists;
	}

	/**
	 * 获取列表
	 * @param int $categoryId
	 * @param int $limit
	 * @return \think\Paginator
	 * @throws \think\db\exception\DbException
	 */
	public function getListShop(int $shopId = 0, $params) {
		// 检索查询条件
		$filter = [];
		// 获取列表数据
		$lists = $this->with(['seat'])

			->where('shop_id', '=', $shopId)
			->where('is_delete', '=', 0)
			->order(['create_time' => 'desc'])
			->select()->toArray();

		return $lists;
	}

}